package com.spark.incoicings.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.service.*;
import com.spark.incoicings.vo.CustomerVO;
import com.spark.invoicings.entity.*;
import com.spark.invoicings.utils.Result;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 销售收款
 *
 */
@RestController
@RequestMapping("/SalesReceipt")
public class SalesReceiptController{
    @Resource
    private SalesReceiptService sr;
    @Resource
    private SalesOrderService so;
    @Resource
    private SaleCustomerService sc;
    @Resource
    private CustomerStatementService css;
    @Resource
    private CustomerStatementDetailService cs;
    @Resource
    private ReceivablePayableDetailService rs;
    @Resource
    private DictDataService ds;
    @Resource
    private OrderDetailService ods;
    @Resource
    private ReceiptService res;
    @Resource
    private ProductService ps;


    /**
     * 销售收款核销条件分页查询
     * */
    @GetMapping("/selectAll")
    public Result selectAll(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "6") Integer pageSize,
            @RequestParam(value = "verificationPerson", required = false) String verificationPerson,
            @RequestParam(value = "paymentMethodId", required = false) Integer paymentMethodId) {

        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("verificationPerson", verificationPerson);
        map.put("paymentMethodId", paymentMethodId);
        PageHelper.orderBy("receipt_id desc");
        List<SalesReceipt> list = sr.selectAll(map);

        for (SalesReceipt s : list) {
            if (s.getCustomerId() != null) {
                s.setSaleCustomer(sc.getById(s.getCustomerId()));
            }
            if (s.getOrderId() != null) {
                s.setSalesOrder(so.getById(s.getOrderId()));
            }
            if (s.getSalesReceiptId() != null) {
                s.setReceipt(res.getById(s.getSalesReceiptId()));
            }
            if (s.getOrderDetailId() != null) {
                s.setOrderDetail(ods.getById(s.getOrderDetailId()));
            }
            if (s.getBillDetailId() != null) {
                s.setCustomerStatementDetail(cs.getById(s.getBillDetailId()));
            }
            if (s.getReceivableDetailId() != null) {
                s.setReceivablePayableDetail(rs.getById(s.getReceivableDetailId()));
            }
            if (s.getPaymentMethodId() != null) {
                s.setDictData(ds.getById(s.getPaymentMethodId()));
            }
        }

        PageInfo<SalesReceipt> page = new PageInfo<>(list);
        return Result.success(200, "success", page);
    }


    /**
     * 查询客户
     * */
    @GetMapping("/selectCustomer")
    public Result selectCustomer(){
        List<SaleCustomer> list= sc.list();
        for (SaleCustomer s:list){
            CustomerVO c = sc.selectCustomerVo(s.getCustomerId());
            s.setBillingMethod(c.getBillingMethod());
            s.setCustomerRating(c.getCustomerRating());
            s.setCustomerSource(c.getCustomerSource());
        }
        return  Result.success(200,"success",list);
    }

    @GetMapping("/selectDict")
    public Result selectDict(String type){
        return  Result.success(200,"success",ds.selectDictType(type));
    }

    /**
     * 根据客户id查询收款单、销售订单、销售订单明细、客户对账单明细、应收明细、
     * */
    @GetMapping("/selectSalesOrder")
    public Result selectSalesOrder(Integer customerId){
        Map<String, Object> result = new HashMap<>();

        List<Receipt> receipts = res.selectId(customerId);
        for(Receipt r:receipts){
            r.setSaleCustomer(sc.getById(r.getCustomerId()));
        }
        result.put("receipts", receipts);

        List<SalesOrder> salesOrders = so.selectId(customerId);
        for (SalesOrder s : salesOrders) {
            s.setDictData(ds.getById(s.getOrderStatusId()));
            s.setCustomer(sc.getById(s.getCustomerId()));
            s.setOrderDetail(ods.selectODetail(s.getOrderId()));
            List<OrderDetail> orderDetail = s.getOrderDetail();
            for (OrderDetail o:orderDetail){
                o.setProducts(ps.getById(o.getProductId()));
            }
        }
        result.put("salesOrders", salesOrders);

        List<CustomerStatement> statements = css.selectId(customerId);
        for (CustomerStatement statement : statements) {
            statement.setCustomerStatementDetails(cs.selectId(statement.getCustomerStatementId()));
        }
        result.put("customerStatements", statements);
        List<ReceivablePayableDetail> list = rs.selectId(customerId);
        for (ReceivablePayableDetail r:list){
            r.setProduct(ps.getById(r.getProductId()));
            r.setDictData(Arrays.asList(ds.getById(r.getSettlementTypeId()), ds.getById(r.getBusinessTypeId())));
        }
        result.put("payableDetails", list);
        return Result.success(200, "success", result);
    }

    /**
     * 根据核销id查询销售收款核销
     * */
    @GetMapping("/SalesReceiptById")
    public Result SalesReceiptById(Integer id){
        SalesReceipt s = sr.getById(id);
        if (s.getCustomerId() != null) {
            s.setSaleCustomer(sc.getById(s.getCustomerId()));
        }
        if (s.getOrderId() != null) {
            s.setSalesOrder(so.getById(s.getOrderId()));
        }
        if (s.getSalesReceiptId() != null) {
            s.setReceipt(res.getById(s.getSalesReceiptId()));
        }
        if (s.getOrderDetailId() != null) {
            s.setOrderDetail(ods.getById(s.getOrderDetailId()));
        }
        if (s.getBillDetailId() != null) {
            s.setCustomerStatementDetail(cs.getById(s.getBillDetailId()));
        }
        if (s.getReceivableDetailId() != null) {
            s.setReceivablePayableDetail(rs.getById(s.getReceivableDetailId()));
        }
        if (s.getPaymentMethodId() != null) {
            s.setDictData(ds.getById(s.getPaymentMethodId()));
        }
        return Result.success(200,"success",s);
    }

    /**
     * 添加销售收款核销
     * */
    @PostMapping("/saveSalesReceipt")
    @Transactional
    public Result saveSalesReceipt(@RequestBody SalesReceipt salesReceipt){
        SalesOrder s = so.getById(salesReceipt.getOrderId());
        double yhk=salesReceipt.getVerificationAmount()+s.getReceivedAmount();//已回款金额
        double dhk=s.getOrderTotalAmount()-yhk;//待回款金额
        s.setReceivedAmount(yhk);
        s.setPendingCollectionAmount(dhk);
        so.updateById(s);
        sr.save(salesReceipt);
        return Result.success();
    }

    /**
     * 修改销售收款核销
     * */
    @PostMapping("/updateSalesReceipt")
    public Result updateSalesReceipt(@RequestBody SalesReceipt salesReceipt){
        sr.updateById(salesReceipt);
        return Result.success();
    }
}
